package com.yuebook.smartrpc.framework.cluster;

import com.google.common.collect.Maps;
import com.yuebook.smartrpc.framework.cluster.impl.HashClusterStrategyImpl;
import com.yuebook.smartrpc.framework.cluster.impl.PollingClusterStrategyImpl;
import com.yuebook.smartrpc.framework.cluster.impl.RondomClusterStrategyImpl;
import com.yuebook.smartrpc.framework.cluster.impl.WeightPollingClusterStrategyImpl;
import com.yuebook.smartrpc.framework.cluster.impl.WeightRandomClusterStrategyImpl;
import java.util.Map;

/**
 * @author honglei
 * @date 2018/11/20 上午10:37
 */
public class ClusterStrategyEngine {

    private static Map<ClusterStrategyEnum, ClusterStrategy> clusterStrategyMap = Maps.newConcurrentMap();

    static {
        clusterStrategyMap.put(ClusterStrategyEnum.CLUSTER_STRATEGY_RANDOM, new RondomClusterStrategyImpl());
        clusterStrategyMap.put(ClusterStrategyEnum.CLUSTER_STRATEGY_WEIGHT_RANDOM, new WeightRandomClusterStrategyImpl());
        clusterStrategyMap.put(ClusterStrategyEnum.CLUSTER_STRATEGY_POLLING, new PollingClusterStrategyImpl());
        clusterStrategyMap.put(ClusterStrategyEnum.CLUSTER_STRATEGY_WEIGHT_POLLING, new WeightPollingClusterStrategyImpl());
        clusterStrategyMap.put(ClusterStrategyEnum.CLUSTER_STRATEGY_HASH, new HashClusterStrategyImpl());
    }

    public static ClusterStrategy queryType(String type) {
        ClusterStrategyEnum eunm = ClusterStrategyEnum.queryByType(type);
        if (eunm == null) {
            return new RondomClusterStrategyImpl();//默认随机算法
        }
        return clusterStrategyMap.get(eunm);
    }
}
